package com.hzl.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzl.lease.common.exception.LeaseException;
import com.hzl.lease.common.result.ResultCodeEnum;
import com.hzl.lease.model.entity.*;
import com.hzl.lease.model.enums.ItemType;
import com.hzl.lease.web.admin.mapper.*;
import com.hzl.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzl.lease.web.admin.vo.attr.AttrValueVo;
import com.hzl.lease.web.admin.vo.graph.GraphVo;
import com.hzl.lease.web.admin.vo.room.RoomDetailVo;
import com.hzl.lease.web.admin.vo.room.RoomItemVo;
import com.hzl.lease.web.admin.vo.room.RoomQueryVo;
import com.hzl.lease.web.admin.vo.room.RoomSubmitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {


    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

//-----
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Override
    public IPage<RoomItemVo> pageItem(Page<Object> objectPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(objectPage, queryVo);
    }

    @Override
    public void saveOrUpdateInfo(RoomSubmitVo roomSubmitVo) {
        // 判断是否是修改，如果传来的id为空，就说明没有房间，则就要添加房间
        boolean isUpdate = roomSubmitVo.getId() != null && roomSubmitVo.getId() != 0;

        //通用的添加修改操作
        this.saveOrUpdate(roomSubmitVo);

        //是修改: 删除房间关联的相关信息
        if (isUpdate) {
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, roomSubmitVo.getId()).eq(GraphInfo::getItemType, ItemType.ROOM));
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, roomSubmitVo.getId()));
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, roomSubmitVo.getId()));
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, roomSubmitVo.getId()));
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId()));
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, roomSubmitVo.getId()));

        }

        /*添加房间图片的信息.具体实现：
            ①先创建一个装所有图片的集合，里面添加的是这个房间的多张图片
            ②判断图片集合不能为null，避免后续出现NullPointerException，因为 CollectionUtils.isEmpty方法
            会处理 null值，即必须最少有一张图片
            ③创建一张图片各种属性的一个集合
            ④循环遍历图片集合，然后为每一张图片的实体对象都添加属性
            ⑥批量处理图片集合，即把装所有图片的集合中的每个图片对象一个个添加属性
        */

        // ①先创建一个装所有图片的集合，里面添加的是这个房间的多张图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        //②判断图片集合不能为null，避免后续出现NullPointerException，因为 CollectionUtils.isEmpty方法会处理 null值，即必须最少有一张图片
        if (!CollectionUtils.isEmpty(graphVoList)) {
            //③创建一张图片各种属性的一个集合
            List<GraphInfo> graphInfoList = new ArrayList<>();
            //④循环遍历图片集合，然后为每一张图片的实体对象都添加属性
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfoList.add(graphInfo);
            }
            //⑥批量处理图片集合，即把装所有图片的集合中的每个图片对象一个个添加属性
            graphInfoService.saveBatch(graphInfoList);
        }


        //添加房间配套的信息
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();

        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<RoomFacility> roomFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }



        //添加房间的属性信息
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }

        //添加房间标签信息
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            List<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }

        //添加房间的租期信息
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                roomLeaseTermList.add(roomLeaseTerm);

            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }

        //添加房间支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        //查询房间的基本信息
        RoomInfo roomInfo = this.getById(id);

        //查询房间所属的公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());

        //房间的图片
        List<GraphVo> graphVoList=graphInfoMapper.selectListByItemIdAndItemType(id, ItemType.ROOM);
        //房间的配套
        List<FacilityInfo> facilityInfoList=facilityInfoMapper.selectListByRoomId(id);
        //查询房间的属性
        List<AttrValueVo> attrValueVoList=attrValueMapper.selectListByRoomId(id);
        //房间的标签
        List<LabelInfo> labelInfoList=labelInfoMapper.selectListByRoomId(id);
        //支付方式
        List<PaymentType> paymentTypeList=paymentTypeMapper.selectListByRoomId(id);
        //租期
        List<LeaseTerm> leaseTermList=leaseTermMapper.selectListByRoomId(id);



        RoomDetailVo roomDetailVo=new RoomDetailVo();

        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);


        return roomDetailVo;
    }

    @Override
    public void removeByIdInfo(Long id) {

        Long count = leaseAgreementMapper.selectCount(new LambdaQueryWrapper<LeaseAgreement>()
                .eq(LeaseAgreement::getRoomId, id).in(LeaseAgreement::getStatus, 2, 5));
        if(count>0){
            throw  new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        //删除房间基本信息
        roomInfoMapper.deleteById(id);
        //删除房间相关信息
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.ROOM));
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));

    }
}




